"""
用户相关的 Pydantic 模型
用于 API 请求和响应的数据验证
"""
from typing import Optional
from pydantic import BaseModel, Field, EmailStr
from datetime import datetime
from ..models.user import UserStatus


# 基础用户信息
class UserBase(BaseModel):
    """用户基础信息"""
    username: str = Field(..., min_length=1, max_length=50, description="用户名")
    job_number: str = Field(..., min_length=1, max_length=50, description="工号")
    phone_number: Optional[str] = Field(default=None, max_length=11, description="手机号")
    email: Optional[str] = Field(default=None, description="邮箱")


# 用户创建
class UserCreate(UserBase):
    """创建用户时的数据模型"""
    password: str = Field(..., min_length=6, max_length=50, description="密码")
    role_id: Optional[str] = Field("pt_user", description="角色ID，可为空")


# 用户更新
class UserUpdate(BaseModel):
    """更新用户时的数据模型"""
    username: Optional[str] = Field("pt_user", min_length=1, max_length=50, description="用户名")
    phone_number: Optional[str] = Field(None, max_length=11, description="手机号")
    email: Optional[str] = Field(None, description="邮箱")
    role_id: Optional[str] = Field("pt_user", description="角色ID")
    status: Optional[UserStatus] = Field(None, description="用户状态")


# 角色信息
class RoleInfo(BaseModel):
    """角色信息"""
    role_id: str
    role_name: str

    class Config:
        from_attributes = True


# 用户响应
class UserResponse(UserBase):
    """用户响应数据模型"""
    role_id: Optional[str] = "pt_user"
    role: Optional[RoleInfo] = None
    status: str
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class UserListResponse(BaseModel):
    """用户列表响应数据模型"""
    total: int
    users: list[UserResponse]


# 登录请求
class LoginRequest(BaseModel):
    """登录请求模型"""
    job_number: str = Field(..., description="工号")
    password: str = Field(..., description="密码")


# 登录响应
class LoginResponse(BaseModel):
    """登录响应模型"""
    username: str
    job_number: str
    role_id: Optional[str] = "pt_user"
    accessToken: str
    status: str
    phone_number: Optional[str] = None
    email: Optional[str] = None

    class Config:
        from_attributes = True


# Token 响应
class TokenResponse(BaseModel):
    """Token 响应模型"""
    accessToken: str
    token_type: str = "bearer"